/*   1:    */ package org.jetbrains.io;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.util.AtomicNotNullLazyValue;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.handler.codec.compression.JZlibEncoder;
import io.netty.handler.codec.compression.JdkZlibDecoder;
import io.netty.handler.codec.compression.ZlibWrapper;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.ide.BinaryRequestHandler;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.security.KeyStore;
import java.security.Security;
import java.util.UUID;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */ 
/*  30:    */ @ChannelHandler.Sharable
/*  31:    */ class PortUnificationServerHandler
/*  32:    */   extends Decoder
/*  33:    */ {
/*  34: 39 */   private static final AtomicNotNullLazyValue<SSLContext> a = new AtomicNotNullLazyValue()
/*  35:    */   {
/*  36:    */     @NotNull
/*  37:    */     protected SSLContext compute()
/*  38:    */     {
/*  39: 43 */       String str = Security.getProperty("ssl.KeyManagerFactory.algorithm");
/*  40: 44 */       if (str == null) {
/*  41: 45 */         str = "SunX509";
/*  42:    */       }
/*  43:    */       try
/*  44:    */       {
/*  45: 49 */         KeyStore localKeyStore = KeyStore.getInstance("JKS");
/*  46: 50 */         char[] arrayOfChar = "jetbrains".toCharArray();
/*  47:    */         
/*  48: 52 */         localKeyStore.load(getClass().getResourceAsStream("cert.jks"), arrayOfChar);
/*  49: 53 */         KeyManagerFactory localKeyManagerFactory = KeyManagerFactory.getInstance(str);
/*  50: 54 */         localKeyManagerFactory.init(localKeyStore, arrayOfChar);
/*  51: 55 */         SSLContext localSSLContext = SSLContext.getInstance("TLS");
/*  52: 56 */         localSSLContext.init(localKeyManagerFactory.getKeyManagers(), null, null); SSLContext 
/*  53: 57 */           tmp73_71 = localSSLContext;
/*  54: 57 */         if (tmp73_71 == null) {
/*  55: 57 */           throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/jetbrains/io/PortUnificationServerHandler$1", "compute" }));
/*  56:    */         }
/*  57: 57 */         return tmp73_71;
/*  58:    */       }
/*  59:    */       catch (Exception localException)
/*  60:    */       {
/*  61: 60 */         throw new RuntimeException(localException);
/*  62:    */       }
/*  63:    */     }
/*  64:    */   };
/*  65:    */   private final boolean b;
/*  66:    */   private final boolean c;
/*  67:    */   private final DelegatingHttpRequestHandler d;
/*  68:    */   
/*  69:    */   public PortUnificationServerHandler()
/*  70:    */   {
/*  71: 71 */     this(new DelegatingHttpRequestHandler(), true, true);
/*  72:    */   }
/*  73:    */   
/*  74:    */   private PortUnificationServerHandler(DelegatingHttpRequestHandler paramDelegatingHttpRequestHandler, boolean paramBoolean1, boolean paramBoolean2)
/*  75:    */   {
/*  76: 75 */     this.d = paramDelegatingHttpRequestHandler;
/*  77: 76 */     this.b = paramBoolean1;
/*  78: 77 */     this.c = paramBoolean2;
/*  79:    */   }
/*  80:    */   
/*  81:    */   protected void messageReceived(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf)
/*  82:    */     throws Exception
/*  83:    */   {
/*  84: 82 */     ByteBuf localByteBuf = getBufferIfSufficient(paramByteBuf, 5, paramChannelHandlerContext);
/*  85: 83 */     if (localByteBuf == null) {
/*  86: 84 */       paramByteBuf.release();
/*  87:    */     } else {
/*  88: 87 */       decode(paramChannelHandlerContext, localByteBuf);
/*  89:    */     }
/*  90:    */   }
/*  91:    */   
/*  92:    */   protected void decode(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf)
/*  93:    */     throws Exception
/*  94:    */   {
/*  95: 92 */     ChannelPipeline localChannelPipeline = paramChannelHandlerContext.pipeline();
/*  96: 93 */     if ((this.b) && (SslHandler.isEncrypted(paramByteBuf)))
/*  97:    */     {
/*  98: 94 */       SSLEngine localSSLEngine = ((SSLContext)a.getValue()).createSSLEngine();
/*  99: 95 */       localSSLEngine.setUseClientMode(false);
/* 100: 96 */       localChannelPipeline.addLast(new ChannelHandler[] { new SslHandler(localSSLEngine), new ChunkedWriteHandler(), new PortUnificationServerHandler(this.d, false, this.c) });
/* 101:    */     }
/* 102:    */     else
/* 103:    */     {
/* 104:100 */       int i = paramByteBuf.getUnsignedByte(paramByteBuf.readerIndex());
/* 105:101 */       int j = paramByteBuf.getUnsignedByte(paramByteBuf.readerIndex() + 1);
/* 106:102 */       if ((this.c) && (i == 31) && (j == 139))
/* 107:    */       {
/* 108:103 */         localChannelPipeline.addLast(new ChannelHandler[] { new JZlibEncoder(ZlibWrapper.GZIP), new JdkZlibDecoder(ZlibWrapper.GZIP), new PortUnificationServerHandler(this.d, this.b, false) });
/* 109:    */       }
/* 110:106 */       else if (a(i, j))
/* 111:    */       {
/* 112:107 */         NettyUtil.addHttpServerCodec(localChannelPipeline);
/* 113:108 */         localChannelPipeline.addLast(new ChannelHandler[] { this.d });
/* 114:109 */         if (BuiltInServer.LOG.isDebugEnabled()) {
/* 115:110 */           localChannelPipeline.addLast(new ChannelHandler[] { new ChannelHandlerAdapter()
/* 116:    */           {
/* 117:    */             public void write(ChannelHandlerContext paramAnonymousChannelHandlerContext, Object paramAnonymousObject, ChannelPromise paramAnonymousChannelPromise)
/* 118:    */               throws Exception
/* 119:    */             {
/* 120:113 */               if ((paramAnonymousObject instanceof HttpResponse))
/* 121:    */               {
/* 122:115 */                 HttpResponse localHttpResponse = (HttpResponse)paramAnonymousObject;
/* 123:116 */                 BuiltInServer.LOG.debug("OUT HTTP: " + localHttpResponse.getStatus().code() + " " + localHttpResponse.headers().get("Content-type"));
/* 124:    */               }
/* 125:118 */               super.write(paramAnonymousChannelHandlerContext, paramAnonymousObject, paramAnonymousChannelPromise);
/* 126:    */             }
/* 127:    */           } });
/* 128:    */         }
/* 129:    */       }
/* 130:123 */       else if ((i == 67) && (j == 72))
/* 131:    */       {
/* 132:124 */         paramByteBuf.skipBytes(2);
/* 133:125 */         localChannelPipeline.addLast(new ChannelHandler[] { new CustomHandlerDelegator(null) });
/* 134:    */       }
/* 135:    */       else
/* 136:    */       {
/* 137:128 */         BuiltInServer.LOG.warn("unknown request, first two bytes " + i + " " + j);
/* 138:129 */         paramChannelHandlerContext.close();
/* 139:    */       }
/* 140:    */     }
/* 141:133 */     a(localChannelPipeline);
/* 142:134 */     localChannelPipeline.remove(this);
/* 143:135 */     paramChannelHandlerContext.fireChannelRead(paramByteBuf);
/* 144:    */   }
/* 145:    */   
/* 146:    */   private static void a(ChannelPipeline paramChannelPipeline)
/* 147:    */   {
/* 148:139 */     ChannelHandler localChannelHandler = ChannelExceptionHandler.getInstance();
/* 149:140 */     if ((paramChannelPipeline.last() != localChannelHandler) || (paramChannelPipeline.context(localChannelHandler) == null)) {
/* 150:141 */       return;
/* 151:    */     }
/* 152:144 */     paramChannelPipeline.remove(localChannelHandler);
/* 153:145 */     paramChannelPipeline.addLast(new ChannelHandler[] { localChannelHandler });
/* 154:    */   }
/* 155:    */   
/* 156:    */   private static boolean a(int paramInt1, int paramInt2)
/* 157:    */   {
/* 158:149 */     return ((paramInt1 == 71) && (paramInt2 == 69)) || ((paramInt1 == 80) && (paramInt2 == 79)) || ((paramInt1 == 80) && (paramInt2 == 85)) || ((paramInt1 == 72) && (paramInt2 == 69)) || ((paramInt1 == 79) && (paramInt2 == 80)) || ((paramInt1 == 80) && (paramInt2 == 65)) || ((paramInt1 == 68) && (paramInt2 == 69)) || ((paramInt1 == 84) && (paramInt2 == 82)) || ((paramInt1 == 67) && (paramInt2 == 79));
/* 159:    */   }
/* 160:    */   
/* 161:    */   private static class CustomHandlerDelegator
/* 162:    */     extends Decoder
/* 163:    */   {
/* 164:    */     private static final int a = 16;
/* 165:    */     
/* 166:    */     protected void messageReceived(ChannelHandlerContext paramChannelHandlerContext, ByteBuf paramByteBuf)
/* 167:    */       throws Exception
/* 168:    */     {
/* 169:166 */       ByteBuf localByteBuf = getBufferIfSufficient(paramByteBuf, 16, paramChannelHandlerContext);
/* 170:167 */       if (localByteBuf == null)
/* 171:    */       {
/* 172:168 */         paramByteBuf.release();
/* 173:    */       }
/* 174:    */       else
/* 175:    */       {
/* 176:171 */         UUID localUUID = new UUID(localByteBuf.readLong(), localByteBuf.readLong());
/* 177:172 */         for (BinaryRequestHandler localBinaryRequestHandler : (BinaryRequestHandler[])BinaryRequestHandler.EP_NAME.getExtensions()) {
/* 178:173 */           if (localUUID.equals(localBinaryRequestHandler.getId()))
/* 179:    */           {
/* 180:174 */             ChannelPipeline localChannelPipeline = paramChannelHandlerContext.pipeline();
/* 181:175 */             localChannelPipeline.addLast(new ChannelHandler[] { localBinaryRequestHandler.getInboundHandler() });
/* 182:176 */             PortUnificationServerHandler.a(localChannelPipeline);
/* 183:177 */             localChannelPipeline.remove(this);
/* 184:178 */             paramChannelHandlerContext.fireChannelRead(localByteBuf);
/* 185:179 */             break;
/* 186:    */           }
/* 187:    */         }
/* 188:    */       }
/* 189:    */     }
/* 190:    */   }
/* 191:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.io.PortUnificationServerHandler

 * JD-Core Version:    0.7.0.1

 */